#!/bin/sh
# Copyright (C) 2015 PIVA Software <www.pivasoftware.com>
# 	Author: Anis Ellouze <anis.ellouze@pivasoftware.com>

#common_execute_method_param "$parameter" "$permission" "$get_cmd" "$set_cmd" "xsd:$type" "$forcedinform"
#  $forcedinform should be set to 1 if the parameter is included in the inform message otherwise empty
#  Default of $type = string

#############################
#   Entry point functions   #
#############################

prefix_list="$prefix_list $DMROOT.IP."
entry_execute_method_list="$entry_execute_method_list entry_execute_method_root_IP"

entry_execute_method_root_IP() {
	case "$1" in ""|"$DMROOT."|"$DMROOT.IP."*)
		common_execute_method_obj "$DMROOT.IP." "0"
		common_execute_method_obj "$DMROOT.IP.Interface." "0" "add_ip_iface" "" "ip_device_browse_instances $1"
		common_execute_method_obj "$DMROOT.IP.Diagnostics." "0"
		common_execute_method_obj "$DMROOT.IP.Diagnostics.IPPing." "0" "" "" "entry_execute_method_root_IPPingDiagnostics"
		return 0
		;;
	esac
	return $E_INVALID_PARAMETER_NAME;
}

sub_entry_ip_interface() {
	local j="$2"
	local ip_int="$3"
	case_param "$1" belongto "$DMROOT.IP.Interface.$j." && {
		common_execute_method_obj "$DMROOT.IP.Interface.$j." "0" "" "del_ip_iface $ip_int"
		common_execute_method_param  "$DMROOT.IP.Interface.$j.Enable" "0" "ip_iface_get_ip_enable" "" "xsd:boolean"
		common_execute_method_param  "$DMROOT.IP.Interface.$j.Name" "0" "echo $ip_int"
		common_execute_method_param  "$DMROOT.IP.Interface.$j.Type" "0" "ip_iface_get_ip_type $ip_int"
		common_execute_method_param  "$DMROOT.IP.Interface.$j.IPv4AddressNumberOfEntries" "0" "echo 1" "" "xsd:unsignedInt"
		common_execute_method_obj "$DMROOT.IP.Interface.$j.IPv4Address." "0" "" "" "ipv4_browse_instances $j $ip_int"
		common_execute_method_obj "$DMROOT.IP.Interface.$j.Stats." "0" "" "" "ip_browse_stat $j $ip_int"
		return 0
	}
	return $E_INVALID_PARAMETER_NAME;		
}

sub_entry_ipv4_interface() {
	local j="$1"
	local ip_int="$2"
	case_param "$1" belongto "$DMROOT.IP.Interface.$j.IPv4Address.1." && {
		common_execute_method_param  "$DMROOT.IP.Interface.$j.IPv4Address.1.IPAddress" "1" "ipv4address_iface_get_ip_adress $ip_int" "ipv4address_iface_set_ip_adress $ip_int"
		common_execute_method_param  "$DMROOT.IP.Interface.$j.IPv4Address.1.AddressingType" "0" "ipv4address_iface_get_type_adress $ip_int"
		common_execute_method_param  "$DMROOT.IP.Interface.$j.IPv4Address.1.Enable" "0" "echo 1" "" "xsd:boolean"
		common_execute_method_param "$DMROOT.IP.Interface.$j.IPv4Address.1.SubnetMask" "0" "get_net_mask $ip_int"
		return 0
	}
	return $E_INVALID_PARAMETER_NAME;
}

sub_entry_ip_stat() {
	local j="$1"
	local ip_int="$2"
	case_param "$1" belongto "$DMROOT.IP.Interface.$j.Stats." && {
		common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.BytesSent" "0" "get_tx_byte_stats" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.BytesReceived" "0" "get_rx_byte_stats" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.PacketsSent" "0" "get_tx_packet_stats" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.PacketsReceived" "0" "get_rx_packet_stats" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.ErrorsSent" "0" "get_tx_error_stats" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.ErrorsReceived" "0" "get_rx_error_stats" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.DiscardPacketsSent" "0" "get_tx_discard_stats" "" "xsd:unsignedInt"
		common_execute_method_param "$DMROOT.IP.Interface.$j.Stats.DiscardPacketsReceived" "0" "get_rx_discard_stats" "" "xsd:unsignedInt"
		return 0
	}
	return $E_INVALID_PARAMETER_NAME;
}

#######################################
#     Data model browse instances     #
#######################################

ip_get_max_instance() {
	local max=`UCI_SHOW | grep "ip_int_instance" | cut -d'=' -f2 | sed 's/[^0-9]*//g' | tail -1`
	echo $max
}

ip_device_update_instance() {
	local ip_int="$1"
	local instance=`$UCI_GET easycwmp.$1.ip_int_instance`
	echo $instance
}

ip_device_browse_instances() {
	for ip_int in "eth0" "eth1" "eth2" "ppp"; do
		local j=`ip_device_update_instance $ip_int`
		sub_entry_ip_interface "$1" "$j" "$ip_int"
	done
}

ipv4_browse_instances() {
	sub_entry_ipv4_interface $1 $2
}

ip_browse_stat(){
	get_stat $2
	sub_entry_ip_stat $1 $2
}

#######################################
#   ADD & DEL obj                     #
#######################################

add_ip_iface() {
	local instance=`ip_get_max_instance`
	$UCI_SET  network.ip_interface_$instance=interface
	$UCI_SET  network.ip_interface_$instance.proto=dhcp
	$UCI_SET  network.ip_interface_$instance.ip_int_instance=$((++instance))
	$UCI_COMMIT
	echo $instance
}

del_ip_iface() {
	local iface="$1"
	$UCI_DELETE $iface
	$UCI_COMMIT
	return 0
}

#######################################
#   GET & SET Values                  #
#######################################

ip_iface_get_ip_enable() {
	echo "1"
}

ip_iface_get_ip_type() {
	echo "Normal"
}

ipv4address_iface_get_ip_adress() {
	if [ "$1" = "ppp" ]; then
		local address=`status -v mwan | grep "IP "| awk '{print $4}'`
	else
		local address=`device_info_get_conf_eth $1 IPADDR`
	fi
	echo $address
}

ipv4address_iface_set_ip_adress() {
	if [ "$1" = "ppp" ]; then
		return 1
	fi
	device_info_set_conf_eth $1 IPADDR $2
	return 0
}

ipv4address_iface_get_type_adress() {
	if [ "$1" = "ppp" ]; then
		local proto="dhcp"
	else
		local proto=`device_info_get_conf_eth $1 BOOTPROTO`
	fi
	echo $proto
}

get_net_mask() {
	if [ "$1" = "ppp" ]; then
	  local mask="255.255.255.255"
	else
	  local mask=`device_info_get_conf_eth $1 NETMASK`
	fi
    echo $mask
}

#######################################
#   Stats                             #
#######################################

get_stat() {
	if [ "$1" = "ppp" ]; then
		local name="usb0"
	else
		local name=$1
	fi
	rx_bytes=`ifconfig $name | grep "RX bytes" | awk '{split($2,a,":"); print a[2]}'`
	rx_packets=`ifconfig $name | grep "RX packets" | awk '{split($2,a,":"); print a[2]}'`
	rx_errors=`ifconfig $name | grep "RX packets" | awk '{split($3,a,":"); print a[2]}'`
	rx_dropped=`ifconfig $name | grep "RX packets" | awk '{split($4,a,":"); print a[2]}'`
	tx_bytes=`ifconfig $name | grep "TX bytes" | awk '{split($6,a,":"); print a[2]}'`
	tx_packets=`ifconfig $name | grep "TX packets" | awk '{split($2,a,":"); print a[2]}'`
	tx_errors=`ifconfig $name | grep "TX packets" | awk '{split($3,a,":"); print a[2]}'`
	tx_dropped=`ifconfig $name | grep "TX packets" | awk '{split($4,a,":"); print a[2]}'`
}

get_tx_byte_stats() {
	echo ${tx_bytes:-0}
}

get_rx_byte_stats() {
	echo ${rx_bytes:-0}
}

get_rx_packet_stats() {
	echo ${rx_packets:-0}
}

get_tx_packet_stats() {
	echo ${tx_packets:-0}
}

get_rx_error_stats() {
	echo ${rx_errors:-0}
}

get_tx_error_stats() {
	echo ${tx_errors:-0}
}


get_rx_discard_stats() {
	echo ${rx_dropped:-0}
}

get_tx_discard_stats() {
	echo ${tx_dropped:-0}
}

#######################################
#
######################################
device_info_get_status_if() {
		local if=$1
		local item=$2
		if [ "$item" = "link" ]; then
			ip link show dev $if | awk NR==1'{split($0,a," "); print a[3]}'
		elif [ "$item" = "ip" ]; then
			ip addr show dev $if | awk NR==3'{split($0,a," "); print a[2]}'
		elif [ "$item" = "ip6" ]; then
			ip -6 addr show dev $if | awk NR==2'{split($0,a," "); print a[2]}'
		fi
}

device_info_get_conf_eth() {
		case $1 in
			eth0)
				local if="ETH"
				;;
			eth1)
				local if="ETH2"
				;;
			eth2)
				local if="ETH3"
				;;
			*)
				;;
		esac
		local item=$2
		grep "${if}_${item}=" < /etc/settings.eth  | awk '{split($0,a,"="); print a[2]}'
}

device_info_set_conf_eth() {
		case $1 in
			eth0)
				local if="ETH"
				;;
			eth1)
				local if="ETH2"
				;;
			eth2)
				local if="ETH3"
				;;
			*)
				;;
		esac
		local item=$2
		local value=$3
		sed -i "s/${if}_${item}=.*/${if}_${item}=${value}/" /etc/settings.eth
		/etc/init.d/eth restart
}
device_info_get_status_if() {
		local if=$1
		local item=$2
		if [ "$item" = "link" ]; then
			ip link show dev $if | awk NR==1'{split($0,a," "); print a[3]}'
		elif [ "$item" = "ip" ]; then
			ip addr show dev $if | awk NR==3'{split($0,a," "); print a[2]}'
		elif [ "$item" = "ip6" ]; then
			ip -6 addr show dev $if | awk NR==2'{split($0,a," "); print a[2]}'
		fi
}
